package com.ll.menu.user.menu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.app.BaseActivity;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;
import com.ll.menu.user.app.entity.MenuItem;
import com.ll.menu.user.cart.CartFragment;
import com.ll.menu.user.cart.CartManager;
import com.ll.menu.user.dialog.LoginDialog;
import com.ll.menu.user.dialog.ManagerDialog;
import com.ll.menu.user.food.list.FoodListFragment;
import com.ll.menu.user.history.list.HistoryListActivity;
import com.ll.menu.user.order.OrderFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import butterknife.OnClick;

public class MenuActivity extends BaseActivity {

    @InjectView(R.id.tvMenuSwitch)
    TextView mSwitchView;

    @InjectView(R.id.lvMenuList)
    ListView mListView;

    @InjectView(R.id.tvMenuCart)
    TextView mCartTextView;

    @InjectView(R.id.tvMenuCurrent)
    TextView mCurrentTextView;

    private MenuListAdapter mMenuListAdapter;
    private String mCurrentRoomFormat = "";
    private String mCurrentTableFormat = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_menu;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurrentRoomFormat = App.getInstance().getString(R.string.current_room);
        mCurrentTableFormat = App.getInstance().getString(R.string.current_table);

        mSwitchView.setText(Html.fromHtml("<u>"
                + App.getInstance().getString(R.string.table_switch) + "</u>"));

        mCurrentTextView.setText(String.format(mCurrentRoomFormat,3));

        initMenuList();

        initCartFragment();
    }

    private static final String IMAGES[] = {
            "http://pic48.nipic.com/file/20140912/9885883_210258641000_2.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=981611891,2367714130&fm=200&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1580042574,39691234&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1812575336,1134127677&fm=27&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1515764429,2475024057&fm=200&gp=0.jpg",

            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3496835735,980528270&fm=27&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=140102405,2416210233&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4110292254,3786062811&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1435924374,2945350287&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=225957003,412463480&fm=27&gp=0.jpg",

            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3985104573,3059443104&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1189336443,1576699178&fm=27&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1457286571,3130383840&fm=200&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2289777559,4124896490&fm=27&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3407880127,3169345977&fm=27&gp=0.jpg",

            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=170559922,2689574909&fm=27&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3481593734,1804899900&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2341286685,524536304&fm=27&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1090662466,2329250735&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1139924260,1720904095&fm=27&gp=0.jpg"
    };

    private void initCartFragment() {
        for(int i = 0;i < 20;i++){
            Food food = new Food();
            food.setId(String.valueOf(i * i + 1000));
            food.setName("小炒肉盖饭套餐+蛋炒饭套餐+饮料瓶");
            food.setCount(i % 4 + 1);
            food.setCurrentPrice(33 + i / 10.0f);
            food.setOldPrice(55 + i * i / 10.0f);
            food.setCountDown(i + 100);
            food.setImage(IMAGES[i]);

            CartManager.getInstance().addFood(food);
        }
    }

    private void initMenuList() {
        ArrayList<MenuItem> list = new ArrayList<>();
        for(int i = 0;i < 10;i++){
            MenuItem item = new MenuItem();

            if(i == 0){
                item.setId(MenuItem.TYPE_DISCOUNT);
            }else if(i == 1){
                item.setId(MenuItem.TYPE_SPECIAL);
            }else if(i == 8){
                item.setId(MenuItem.TYPE_SALE);
            }else if(i == 9){
                item.setId(MenuItem.TYPE_ORDER);
            }

            item.setTitle("菜品-" + (i + 1));
            item.setId(String.valueOf(i + 1));
            list.add(item);
        }

        MenuItem first = list.get(0);

        first.setSelect(true);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.flMenuFragmentContainer,
                FoodListFragment.getInstance(fm,first.getId(),first.getTitle()));
        ft.commitAllowingStateLoss();


        mMenuListAdapter = new MenuListAdapter();
        mMenuListAdapter.resetDataList(list);

        mListView.setAdapter(mMenuListAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mMenuListAdapter == null || mMenuListAdapter.getCount() == 0){
                    return;
                }
                if(position < 0 || position >= mMenuListAdapter.getCount()){
                    return;
                }
                List<MenuItem> list = mMenuListAdapter.getItemList();
                if(list == null){
                    return;
                }
                if(list.get(position).isSelect()){
                    return;
                }
                int len = list.size();
                for(int i = 0;i < len;i++){
                    MenuItem item = list.get(i);
                    if(i == position){
                        item.setSelect(true);

                        Fragment fragment;
                        FragmentManager fm = getSupportFragmentManager();

                        if(item.isDiscount()){
                            fragment = FoodListFragment.getInstance(fm,item.getId(),item.getTitle());
                        }else if(item.isOrder()){
                            fragment = OrderFragment.getInstance(getSupportFragmentManager());
                        }else if(item.isSpecial()){
                            fragment = FoodListFragment.getInstance(fm,item.getId(),item.getTitle());
                        }else if(item.isSale()){
                            fragment = FoodListFragment.getInstance(fm,item.getId(),item.getTitle());
                        }else{
                            fragment = FoodListFragment.getInstance(fm,item.getId(),item.getTitle());
                        }

                        FragmentTransaction ft = fm.beginTransaction();
                        ft.replace(R.id.flMenuFragmentContainer, fragment);
                        ft.commitAllowingStateLoss();

                    }else{
                        item.setSelect(false);
                    }
                }
                mMenuListAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.tvMenuSwitch,R.id.tvMenuLogin,R.id.tvMenuCart,R.id.tvMenuOrderList})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvMenuSwitch:
                new ManagerDialog(MenuActivity.this).show();
                break;
            case R.id.tvMenuLogin:
                new LoginDialog(MenuActivity.this).show();
                break;
            case R.id.tvMenuOrderList:
                HistoryListActivity.launch(MenuActivity.this);
                break;
            case R.id.tvMenuCart:
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.flMenuFragmentContainer, CartFragment.getInstance(getSupportFragmentManager()));
                ft.commitAllowingStateLoss();
                break;
            default:

                break;
        }
    }

    public static void launch(Activity activity){
        Intent intent = new Intent(activity,MenuActivity.class);
        activity.startActivity(intent);
    }
}
